package com.elitel.frame.web.cfgsiminfo.service.impl;

import com.elitel.common.utils.StringUtils;
import com.elitel.common.utils.SysUserUtils;
import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.web.cfgsiminfo.dao.CfgSimQueryFieldDao;
import com.elitel.frame.web.cfgsiminfo.entity.CfgSimQueryField;
import com.elitel.frame.web.cfgsiminfo.entity.CfgSimQueryFieldListDTO;
import com.elitel.frame.web.cfgsiminfo.service.CfgSimQueryFieldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author mulis
 */
@Service
public class CfgSimQueryFieldServiceImpl implements CfgSimQueryFieldService {
    @Resource
    private CfgSimQueryFieldDao cfgSimQueryFieldDao;

    @Override
    public int deleteByPrimaryKey(String queryFieldId) {
        return cfgSimQueryFieldDao.deleteByPrimaryKey(queryFieldId);
    }

    @Override
    public int insert(CfgSimQueryField record) {
        return cfgSimQueryFieldDao.insert(record);
    }

    @Override
    public int insertSelective(CfgSimQueryField record) {
        return cfgSimQueryFieldDao.insertSelective(record);
    }

    @Override
    public CfgSimQueryField selectByPrimaryKey(String queryFieldId) {
        return cfgSimQueryFieldDao.selectByPrimaryKey(queryFieldId);
    }

    @Override
    public int updateByPrimaryKeySelective(CfgSimQueryField record) {
        return cfgSimQueryFieldDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CfgSimQueryField record) {
        return cfgSimQueryFieldDao.updateByPrimaryKey(record);
    }

    @Override
    public List<CfgSimQueryField> selectList(CfgSimQueryField cfgSimQueryField) {
        return cfgSimQueryFieldDao.selectList(cfgSimQueryField);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(CfgSimQueryFieldListDTO dto) {
        String serviceId = dto.getServiceId();
        // 先删除
        cfgSimQueryFieldDao.deleteByServiceId(serviceId);

        Date date = new Date();
        String userId = SysUserUtils.getLoginAccount();
        // 再保存
        List<CfgSimQueryField> list = dto.getList();
        for (int i = 0; i < list.size(); i++) {
            CfgSimQueryField cfgSimQueryField = list.get(i);
            cfgSimQueryField.setQueryFieldId(UUIDUtil.randomUUID());
            cfgSimQueryField.setServiceId(serviceId);
            if (StringUtils.isBlank(cfgSimQueryField.getSourceFieldCode())) {
                throw new RuntimeException("源参数Code不能为空");
            }
            if (StringUtils.isBlank(cfgSimQueryField.getSourceFieldName())) {
                cfgSimQueryField.setSourceFieldName(cfgSimQueryField.getSourceFieldCode());
            }
            if (StringUtils.isBlank(cfgSimQueryField.getNewFieldCode())) {
                cfgSimQueryField.setNewFieldCode(cfgSimQueryField.getSourceFieldCode());
            }
            if (StringUtils.isBlank(cfgSimQueryField.getNewFieldName())) {
                cfgSimQueryField.setNewFieldName(cfgSimQueryField.getSourceFieldName());
            }
            cfgSimQueryField.setCreatedTime(date);
            cfgSimQueryField.setCreatedUser(userId);
            cfgSimQueryField.setUpdatedTime(date);
            cfgSimQueryField.setUpdatedUser(userId);
            cfgSimQueryField.setSortNum(i + 1);
            cfgSimQueryFieldDao.insertSelective(cfgSimQueryField);
        }
    }
}
